import { useContext } from 'react';
import { getLoadMapList, getTenantID } from '@/utils';
import { ILoadMapTypeAll } from '@/common';
import { useDescriptionsType } from '@/components/DescriptionsType';

import { NeedControllerContext } from '../common';
import { RegularExpression } from '@/common/constVariable';
import { NeedManageAPI } from '../common/type';

const getDescrConfig = ({ updateForm }: NeedManageAPI.IUpdateDesConfig) => {
  /** 获取上下文 */
  const { pageType, needForm, needDetailInfo } = useContext(NeedControllerContext);

  /**  获取数据字典 */
  const { loadMapInfo, mapLoading } = getLoadMapList([
    `${getTenantID()}${ILoadMapTypeAll.PURCHASE_CATEGORY}`,
    `${getTenantID()}${ILoadMapTypeAll.MATERIAL_MIDI}`,
  ]);

  /** 表单配置 */
  const [config, methods] = useDescriptionsType({
    labelWidth: 190,
    basicForm: updateForm ? updateForm : needForm,
    name: 'need-addAndEdit',
    schemas: [
      {
        key: 'need-basic',
        title: '基本信息',
        fieldChildren: [
          {
            field: `fullName`,
            label: `标的名称`,
            component: 'Input',
            placeholder: '请输入',
            required: true,
            message: '请输入标的名称',
          },
          {
            field: `supplyType`,
            label: `标的类别`,
            component: 'Select',
            placeholder: '请选择',
            options: loadMapInfo?.[`${getTenantID()}${ILoadMapTypeAll.PURCHASE_CATEGORY}`],
            showArrow: false,
            initialValue: needDetailInfo?.supplyType ? needDetailInfo?.supplyType : null,
            fieldNames: { label: 'name', value: 'id' },
            loading: mapLoading,
            required: true,
            message: '请选择标的类别',
          },
          {
            field: `needParams`,
            label: `规格要求（参数）`,
            component: 'Input',
            // disabled: true,
            placeholder: '请输入',
            pattern: RegularExpression?.noChinese,
            patternMsg: '规格参数只能填字母和数字！',
            required: true,
            message: '请输入规格要求（参数）',
          },
          {
            field: `unitBusiness`,
            label: `量化单位`,
            component: 'Select',
            placeholder: '请选择',
            options: loadMapInfo?.[`${getTenantID()}${ILoadMapTypeAll.MATERIAL_MIDI}`],
            showArrow: false,
            initialValue: needDetailInfo?.unitBusiness ? needDetailInfo?.unitBusiness : null,
            fieldNames: { label: 'name', value: 'id' },
            loading: mapLoading,
            required: true,
            message: '请选择量化单位',
          },
          {
            field: `expectPrice`,
            label: `预估单价`,
            component: 'InputNumber',
            placeholder: '请输入',
            // required: pageType == 'detail' ? false : true,
            // disabled: pageType == 'detail' ? true : false,
            min: 0,
            contentAfter: '元',
            initialValue: undefined,
            precision: 2,
            required: true,
            message: '请输入预估单价',
          },
          {
            field: `expectNum`,
            label: `需求数量`,
            component: 'Input',
            placeholder: '请输入',
            pattern: RegularExpression?.integerNumber,
            patternMsg: '需求数量必须为正整数！',
            required: true,
            message: '请输入需求数量',
          },
          {
            field: `importFlag`,
            label: `是否进口`,
            component: 'Select',
            placeholder: '请选择',
            options: [
              {
                value: 0,
                label: '否',
              },
              {
                value: 1,
                label: '是',
              },
            ],
            showArrow: false,
            initialValue: needDetailInfo?.importFlag ? needDetailInfo?.importFlag : null,
          },
          {
            field: `singleFlag`,
            label: `是否单一来源`,
            component: 'Select',
            placeholder: '请选择',
            options: [
              {
                value: 0,
                label: '否',
              },
              {
                value: 1,
                label: '是',
              },
            ],
            showArrow: false,
            initialValue: needDetailInfo?.importFlag ? needDetailInfo?.importFlag : null,
          },
          {
            field: `expectTime`,
            label: `计划采购时间`,
            component: 'DatePicker',
            format: 'YYYY-MM-DD',
            placeholder: pageType == 'detail' ? '--' : '请选择',
            // required: pageType == 'detail' ? false : true,
            // disabled: pageType == 'detail' ? true : false,
            // onChange: (date: any) => {
            //   setPreBeginTimeStart(date);
            // },
          },
        ],
      },
    ],
  });
  return {
    config,
    methods,
  };
};

export { getDescrConfig };
